//
//  AuthenticationViewController.m
//  UniversalApp
//
//  Created by 志刚杨 on 2018/8/20.
//  Copyright © 2018年 voidcat. All rights reserved.
//

#import "AuthenticationViewController.h"
#import "HFFormHelper.h"
#import "BRPickerView.h"
#import "MineLogic.h"
#import "QiNiuLogic.h"
#import "QiNiuInfoModel.h"
#import "HFFormAlbumTVC.h"
#import "HFFormAlbumModel.h"
@interface AuthenticationViewController ()<HFFormDelegate>
@property(nonatomic, strong) QiNiuInfoModel *qiniuInfo;
@property(nonatomic, strong) NSString *IDImage1;
@property(nonatomic, strong) NSString *IDImage2;

@end

@implementation AuthenticationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self construcDatas];
}

- (void)construcDatas{
    HFFormSectionModel *section;
    HFFormRowModel *row;
    
    section         = [HFForm section];
    row             = [HFForm rowWithType:HFFormRowTypeDefault];
    row.title       = ASLocalizedString(@"昵称");
    row.placeholder = ASLocalizedString(@"请输入昵称（必填）");
    row.key = @"nickname";
    row.neccesary = YES;
    [section appendRow:row];
    
    row                 = [HFForm rowWithType:HFFormRowTypeDefault];
    row.title           = ASLocalizedString(@"手机");
    row.placeholder     = ASLocalizedString(@"请输入手机号码");
    row.keyboardType    = UIKeyboardTypeNumberPad;
    row.key = @"phone";
    row.neccesary = YES;
    [section appendRow:row];
    
    
    row                 = [HFForm rowWithType:HFFormRowTypeOptions];
    row.title           = ASLocalizedString(@"身高");
    row.placeholder     = ASLocalizedString(@"请选择身高");
    row.neccesary = YES;
    row.subRowsHandler  = ^{
        NSMutableArray *array = @[].mutableCopy;
        HFFormRowModel *relRow = [HFForm row];
        relRow.key = @"height";

        NSMutableArray *heights = [NSMutableArray array];
        for (int i=130; i<230; i++) {
            [heights addObject:StringFromInt(i)];
        }
        relRow.multiDatas = heights;
        [array addObject:relRow];
        return array;
    };

    row.key = @"height";
    [section appendRow:row];
    
    
    row                 = [HFForm rowWithType:HFFormRowTypeOptions];
    row.title           = ASLocalizedString(@"体重(kg)");
    row.placeholder     = ASLocalizedString(@"请选择体重");
    row.neccesary = YES;
    row.subRowsHandler  = ^{
        NSMutableArray *array = @[].mutableCopy;
        HFFormRowModel *relRow = [HFForm row];
        relRow.key = @"weight";
        NSMutableArray *heights = [NSMutableArray array];
        for (int i=30; i<100; i++) {
            [heights addObject:StringFromInt(i)];
        }
        relRow.multiDatas = heights;
        [array addObject:relRow];
        return array;
    };
//    row.key = @"weight";
    [section appendRow:row];
    
    
    row                 = [HFForm rowWithType:HFFormRowTypeOptions];
    row.title           = ASLocalizedString(@"星座");
    row.placeholder     = ASLocalizedString(@"请选择星座");
    row.neccesary = YES;
    row.subRowsHandler  = ^{
        NSMutableArray *array = @[].mutableCopy;
        HFFormRowModel *relRow = [HFForm row];
        relRow.key = @"constellation";

        relRow.multiDatas = @[ASLocalizedString(@"白羊座"),
        ASLocalizedString(@"金牛座"),
        ASLocalizedString(@"双子座"),
        ASLocalizedString(@"巨蟹座"),
        ASLocalizedString(@"狮子座"),
        ASLocalizedString(@"处女座"),
        ASLocalizedString(@"天秤座"),
        ASLocalizedString(@"天蝎座"),
        ASLocalizedString(@"射手座"),
        ASLocalizedString(@"魔羯座"),
        ASLocalizedString(@"水瓶座"),
                              ASLocalizedString(@"双鱼座")];
        [array addObject:relRow];
        return array;
    };
//    row.key = @"constellation";
    [section appendRow:row];
    
    row             = [HFForm rowWithType:HFFormRowTypeLocation];
    row.title       = ASLocalizedString(@"所在城市");
    row.placeholder     = ASLocalizedString(@"请选择城市");
    row.key = @"city";
    row.neccesary = YES;
    [section appendRow:row];

    row             = [HFForm rowWithType:HFFormRowTypeDefault];
    row.title       = ASLocalizedString(@"个人介绍");
    row.placeholder = ASLocalizedString(@"必填");
    row.neccesary = YES;
    row.key = @"introduce";

    [section appendRow:row];

    row                 = [HFForm rowWithType:HFFormRowTypeTags];
    row.title           = ASLocalizedString(@"形象标签");
    row.multiDatas      = @[ASLocalizedString(@"情感专家"), ASLocalizedString(@"清纯美女"), ASLocalizedString(@"热于助人"), ASLocalizedString(@"宅男女神"), ASLocalizedString(@"邻家小妹")];
    row.key = @"image_label";
    row.neccesary = YES;

    [section appendRow:row];

    row             = [HFForm rowWithType:HFFormRowTypeDefault];
    row.title       = ASLocalizedString(@"个人签名");
    row.placeholder = ASLocalizedString(@"必填");
    row.key = @"self_label";
    row.neccesary = YES;
    [section appendRow:row];
    
    row             = [HFForm rowWithType:HFFormRowTypeAlbum];
    row.title       = ASLocalizedString(@"身份证人像面");
    row.placeholder = ASLocalizedString(@"必填");
    row.key = @"auth_id_card_img_url1_img";
    row.neccesary = YES;
    row.valueHandler = ^(id value) {
        HFFormAlbumModel *cellModel = value;
        if(cellModel.photos.count > 0){
            [[HUDHelper sharedInstance] syncLoading];
            HFFormAlbumPhotoModel *photo = cellModel.photos[0];
            [QiNiuLogic QiNiuUplaodImage:photo.thumbnailImage WithQiuNiuInfo:_qiniuInfo  Block:^(id selfPtr) {
                [[HUDHelper sharedInstance] syncStopLoading];
                _IDImage1 =[NSString  stringWithFormat:@"%@/%@",((QiNiuInfoModel *)selfPtr).domain,((QiNiuInfoModel *)selfPtr).fileKey];
            }];
        }
    };
    row.settingsHandler = ^NSDictionary *{
        return @{@"maxCount":@"1"};
    };
    [section appendRow:row];
    
    row             = [HFForm rowWithType:HFFormRowTypeAlbum];
    row.title       = ASLocalizedString(@"身份证国徽面");
    row.placeholder = ASLocalizedString(@"必填");
    row.key = @"auth_id_card_img_url2_img";
    row.neccesary = YES;
    row.settingsHandler = ^NSDictionary *{
        return @{@"maxCount":@"1"};
    };
    
    row.valueHandler = ^(id value) {
        HFFormAlbumModel *cellModel = value;
        if(cellModel.photos.count > 0){
            [[HUDHelper sharedInstance] syncLoading];
            HFFormAlbumPhotoModel *photo = cellModel.photos[0];
            [QiNiuLogic QiNiuUplaodImage:photo.thumbnailImage WithQiuNiuInfo:_qiniuInfo  Block:^(id selfPtr) {
                [[HUDHelper sharedInstance] syncStopLoading];
                _IDImage2 =[NSString  stringWithFormat:@"%@/%@",((QiNiuInfoModel *)selfPtr).domain,((QiNiuInfoModel *)selfPtr).fileKey];
            }];
        }
    };
    [section appendRow:row];
    
    row                 = [HFForm rowWithType:HFFormRowTypeSubmit];
    row.title           = ASLocalizedString(@"提交审核");
//    row.neccesary = YES;

    [section appendRow:row];

    
    [self.form appendSection:section];
    self.form.delegate = self;
    
    [self.form reloadData];
}



-(void)form:(HFForm *)form didSelectRowAtIndexPath:(NSIndexPath *)indexPath rowModel:(HFFormRowModel *)row tableViewCell:(UITableViewCell *)cell
{
//    [self.form reloadData];
    if([row.title isEqualToString:ASLocalizedString(@"所在城市")])
    {
        NSString * path = [[NSBundle mainBundle] pathForResource:@"region.plist" ofType:nil];
        NSArray *city = [NSArray arrayWithContentsOfFile:path];
        [BRAddressPickerView showAddressPickerWithShowType:BRAddressPickerModeCity dataSource:city defaultSelected:nil isAutoSelect:NO themeColor:nil resultBlock:^(BRProvinceModel *province, BRCityModel *city, BRAreaModel *area) {

            row.content = [NSString stringWithFormat:@"%@-%@",province.name,city.name];
            row.value = [NSString stringWithFormat:@"%@-%@",province.name,city.name];
            [self.form reloadData];
        } cancelBlock:^{
            
        }];
    }
    
    if([row.title isEqualToString:ASLocalizedString(@"提交审核")])
    {
        //循环判断下必填
        for(int i =0;i<form.getAllRows.count;i++)
        {
            HFFormRowModel *crow = form.getAllRows[i];
            NSLog(@"row value %@",crow.value);
//            if(crow.value == nil && crow.content != nil)
//            {
//                crow.content = crow.content;
//            }
//
            if(crow.value == nil && crow.neccesary == YES)
            {
                [[HUDHelper sharedInstance] tipMessage:[NSString stringWithFormat:ASLocalizedString(@"必须要填写 %@"),crow.title]];
                return;
            }

        }
        NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:[self.form exportDataToDictionary]];
        [dic setObject:_IDImage1 forKey:@"auth_id_card_img_url1"];
        [dic setObject:_IDImage2 forKey:@"auth_id_card_img_url2"];
//        NSDictionary *dic =  [self.form exportDataToDictionary];
        if(_IDImage1 == nil)
        {
            [[HUDHelper sharedInstance] tipMessage:[NSString stringWithFormat:ASLocalizedString(@"请上传身份证人像面")]];
            return;
        }
        
        if(_IDImage2 == nil)
        {
            [[HUDHelper sharedInstance] tipMessage:[NSString stringWithFormat:ASLocalizedString(@"请上传身份证人像面")]];
            return;
        }
        
        [MineLogic requestSubmitAuthInfoWith:dic andBlock:^(id selfPtr) {
            [MBProgressHUD showTopTipMessage:ASLocalizedString(@"提交成功")];
            [self.navigationController popViewControllerAnimated:YES];
        }];
    }
    else if([row.title isEqualToString:ASLocalizedString(@"国徽")])
    {

    }
    else if([row.title isEqualToString:ASLocalizedString(@"人像")])
    {
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [QiNiuLogic getQiniuTokenSuccess:^(id selfPtr) {
        _qiniuInfo = selfPtr;
        NSLog(@"%@",_qiniuInfo);
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
